'use strict'

process.chdir(__dirname)

const cdpc = require('../index')

const cm = new cdpc({
  loadInfoFile: '--mem',
  debug: true
})

console.log(process.pid)

cm.runChilds([
  {
    name: 'date',
    command: 'date',
    restart: 'count',
    restartDelay: 1000,
    restartLimit: 0,
    autoRemove: true,
    options: {
      stdio: ['ignore', 1, 2]
    }
  },
  {
    name: 'httpclient',
    file: 'hcli.js',
    restart: 'count',
    restartDelay: 10,
    restartLimit: 50,
    options: {
      stdio: ['ignore', 1, 2]
    },
    after: [
      'httpserver'
    ]
  },
  
  {
    name : 'httpserver',
    file: 'app1.js',
    restart: 'always',
    restartDelay: 1000,
    args: [
      '--strong'
    ],
    options: {
      stdio: ['ignore', 1, 2, 'ipc']
    },
    limit: {
      maxrss: 60000000
    },
    monitorNetData: true,
    callback: (ch, cm, chk) => {
      ch.on('message', (msg, w) => {
        console.log('child get message', msg)
      })

      ch.on('exit', () => {
        console.log(chk.cause)
      })

      ch.send({
        pid: ch.pid
      })
    }
  },

  {
    name: '',
    command: 'uname',
    args: [ '-a' ],
    restart: 'count',
    restartLimit: 2,
    //autoRemove: true,
    options: {
      stdio: ['ignore', 1, 2]
    },
    disabled: true
  },

  {
    name: '',
    command: 'uname',
    args: [ '-nmv' ],
    restart: 'count',
    restartLimit: 2,
    //autoRemove: true,
    options: {
      stdio: ['ignore', 1, 2]
    },
    disabled: true
  },
  
  /* {
    name: 'postgresql',
    command: `/usr/local/pgsql-16.x/bin/postgres`,
    args: [
      '-D', '/usr/local/pgsql-16.x-data'
    ],
    only: true,
    onlyArgs: ['-D', '/usr/local/pgsql-16.x-data']
  } */

])

cm.dynamicStep = 3
cm.setStepSlice(5)
cm.setMaxStep(2, 105)
cm.monitorStart()

queueMicrotask(() => {
  let hs = cm.find('httpserver')

  setTimeout(() => {
    console.log(hs.loadinfo)
  }, 500)

  setTimeout(() => {
    console.log(hs.loadinfo)
  }, 900)

  setTimeout(() => {
    console.log(hs.loadinfo)
  }, 1000)

  setTimeout(() => {
    console.log(hs.loadinfo)
  }, 1200)

  setTimeout(() => {
    console.log(hs.loadinfo)
  }, 1500)

})

setTimeout(() => {
  console.log('检测date应用是否存在...')
  let a = cm.find('date')

  console.log(a ? a : 'null')
}, 4500)

/* setTimeout(() => {
  console.log('httpserver stoped')
  cm.stop('httpserver')
  cm.find('httpserver').limit.maxrss = 60000
}, 5000)
 */
setTimeout(() => {
  console.log('httpserver stoped')
  cm.start('httpserver')
}, 6000)

console.log('15秒后暂停http server (name=httpserver)，并且在25秒后恢复服务...')

/* setTimeout(() => {
  console.log('httpserver paused')
  cm.pause('httpserver')
}, 15000)
 */
setTimeout(() => {
  console.log('httpserver resume')
  cm.resume('httpserver')
}, 25000)

setTimeout(() => {
  console.log('uname enabled')
  let chs = cm.findByCommand('uname')
  for (let n of chs) {
    console.log('enable', n)
    cm.enable(n)
    cm.start(n)
  }
}, 2500)

setTimeout(() => {
  console.log('测试完成，主进程退出。')
  cm.killAllChilds(process.pid, 'SIGKILL')
  process.exit(0)
}, 30000);
